package com.wupo.demo.controller;

import com.wupo.demo.entity.User;
import com.wupo.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SM用户表 Controller
 *
 * @author wupo
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/user")
@Api(tags = "UserController",description = "SM用户表CURD")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "list", notes = "SM用户表列表", httpMethod = "GET")
    @GetMapping
    public BaseResponseBean list() {
        return BaseResponseBean.success(userService.list());
    }

    @ApiOperation(value = "getUser",notes = "根据UserID获取实例",httpMethod = "GET")
    @ApiImplicitParam(required = true, name = "id", defaultValue = "1")
    @GetMapping("/get/{id}")
    public BaseResponseBean get(@PathVariable("id") Long id){
        return BaseResponseBean.success(userService.getById(id));
    }

    @ApiOperation(value = "addUser",notes = "新增实例",httpMethod = "POST")
    @PostMapping("/add")
    public BaseResponseBean add(@RequestBody User user){
        return BaseResponseBean.success(userService.save(user));
    }

    @ApiOperation(value = "updateUser",notes = "更新实例")
    @PutMapping("/update")
    public BaseResponseBean updateById(@RequestBody User user){
        return BaseResponseBean.success(userService.updateById(user));
    }

    @ApiOperation(value = "saveAndUpdateUser",notes = "新增或更新实例",httpMethod = "POST")
    @PostMapping
    public BaseResponseBean addOrUpdate(@RequestBody User user){
        return BaseResponseBean.success(userService.saveOrUpdate(user));
    }

    @ApiOperation(value ="removeSysDeptUser",notes = "移除实例",httpMethod = "DELETE")
    @DeleteMapping("/del")
    public BaseResponseBean remove(@RequestBody User user){
        return BaseResponseBean.success(userService.removeById(user));
        }

}
